package com.yami.shop.consign.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.ProdConsignStatus;
import com.yami.shop.bean.vo.*;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageUtil;
import com.yami.shop.consign.common.dao.ProdConfigMapper;
import com.yami.shop.consign.common.model.ProdConfig;
import com.yami.shop.consign.common.service.ConsignAnalysisService;
import com.yami.shop.consign.common.service.ProdConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ProdConfigServiceImpl extends ServiceImpl<ProdConfigMapper, ProdConfig> implements ProdConfigService {

    @Autowired
    private ProdConfigMapper prodConfigMapper;

    @Autowired
    private ConsignAnalysisService consignAnalysisService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertProd(ProdConfig prodConfig) {
        ProdConfig one = getOne(new LambdaQueryWrapper<ProdConfig>().eq(ProdConfig::getProdId, prodConfig.getProdId()).ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue()));
        if (Objects.nonNull(one)) {
            throw new YamiShopBindException("该商品已参与寄售！");
        }
        save(prodConfig);
        consignAnalysisService.initialSave(prodConfig.getProdId());
    }

    @Override
    public PageVO<ProdConfig> getPage(PageDTO pageDTO, ProdConfig prodConfig) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String format = Objects.nonNull(prodConfig.getCreateTime()) ? fmt.format(prodConfig.getCreateTime()) : null;
        PageVO<ProdConfig> pageVO = PageUtil.doPage(pageDTO, 
                () -> list(new LambdaQueryWrapper<ProdConfig>()
                .like(StringUtils.isNotEmpty(prodConfig.getProdName()), ProdConfig::getProdName, prodConfig.getProdName())
                .eq(Objects.nonNull(prodConfig.getStatus()), ProdConfig::getStatus, prodConfig.getStatus())
                .apply(StringUtils.isNotEmpty(format), "TO_DAYS(create_time) = TO_DAYS('" + format + "')")
                .ne(ProdConfig::getStatus, ProdConsignStatus.DELETE.getValue())
                .orderByDesc(ProdConfig::getCreateTime)));
        return pageVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(Long[] ids) {
        for (Long id : ids) {
            ProdConfig prodConfig = getById(id);
            if (prodConfig.getStatus() != ProdConsignStatus.CLOSE.getValue()) {
                throw new YamiShopBindException("["+prodConfig.getProdName()+"]寄售关闭后才可删除！");
            }
            prodConfig.setStatus(ProdConsignStatus.DELETE.getValue());
            updateById(prodConfig);
        }
    }

    @Override
    public PageVO<ProdConsignVO> getAppProdList(PageDTO pageDTO, Integer type, String prodName, Integer SeriesId) {
        // 0 时间降叙 1 时间升序 2 价格降序 3 价格升序
        PageVO<ProdConsignVO> pageVO = PageUtil.doPage(pageDTO, () -> prodConfigMapper.getprodList(type,prodName,SeriesId));
        List<ProdConsignVO> list = pageVO.getList();
        if(CollUtil.isNotEmpty(list)){
            Map<Integer, List<ProdConsignVO>> map = list.stream().filter(prod -> prod.getCirculateNum() == 0).collect(Collectors.groupingBy(ProdConsignVO::getAttr));
            
            for(ProdConsignVO prodConsignVO:pageVO.getList()){
                //如果运维端没有配置流通数,取币得使用数量
                if(prodConsignVO.getCirculateNum() ==0){
                    // 类型盲盒 6
                    if(prodConsignVO.getAttr() == 6){
//                        ServerResponseEntity<Integer> countBydenomId = nftFeignClient.getCountBydenomId(prodConsignVO.getDenomId());
//                        if(countBydenomId.isSuccess()){
//                            prodConsignVO.setCirculateNum(countBydenomId.getData());
//                        }
                    }
                    //普通商品 1
                    if(prodConsignVO.getAttr() == 1){
//                        ServerResponseEntity<Integer> countBydawNameId = nftFeignClient.getCountBydawNameId(prodConsignVO.getDawNameId());
//                        if(countBydawNameId.isSuccess()){
//                            prodConsignVO.setCirculateNum(countBydawNameId.getData());
//                        }
                    }
                }
            }
        }

        return pageVO;
    }

    @Override
    public PageVO<ProdConsignInfoVO> getAppProdInfoList(PageDTO pageDTO, Integer type, Integer prodId) {
        // 0 时间降叙 1 时间升序 2 价格降序 3 价格升序   4  编号降序 5 编码升序
        PageVO<ProdConsignInfoVO> appProdList = PageUtil.doPage(pageDTO, () -> prodConfigMapper.getAppProdInfoList(type,prodId));
        return appProdList;
    }

    @Override
    public ProdConfigVO getInfoByproudctId(Integer productId) {
        ProdConfig prodConfig = prodConfigMapper.selectOne(new QueryWrapper<ProdConfig>().eq("prod_id", productId).ne("status", ProdConsignStatus.DELETE.getValue()));
        ProdConfigVO prodConfigVO = BeanUtil.copyProperties(prodConfig, ProdConfigVO.class);
        return prodConfigVO;
    }


    @Override
    public List<DingProduct> getDingProdList() {
        return prodConfigMapper.getDingProdList();
    }

    @Override
    public List<DingMarket> getMarketByDing() {
        return prodConfigMapper.getMarketByDing();
    }


}
